package io.gbznt.modules.buss.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.entity.SysDictEntity;
import io.gbznt.modules.sys.service.SysDeptService;
import io.gbznt.modules.sys.service.SysDictService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.gbznt.common.utils.DateUtils;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.common.validator.ValidatorUtils;
import io.gbznt.common.validator.group.AddGroup;
import io.gbznt.modules.buss.constatnt.ProcessStateEnum;
import io.gbznt.modules.buss.entity.BussMaintainTaskEntity;
import io.gbznt.modules.buss.entity.BussRtuEquipmentEntity;
import io.gbznt.modules.buss.entity.BussRtuInfoEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussMaintainTaskService;
import io.gbznt.modules.buss.service.BussRtuEquipmentService;
import io.gbznt.modules.buss.service.BussRtuInfoService;
import io.gbznt.modules.sys.entity.SysUserEntity;
import io.gbznt.modules.sys.service.SysUserService;

/**
 * 工单任务
 *
 * @author gbznt
 * @email http://www.gbznt.com/
 * @date 2019-04-20 15:48:33
 */
@RestController
@RequestMapping("buss/bussmaintaintask")
public class BussMaintainTaskController {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private BussMaintainTaskService bussMaintainTaskService;
	@Autowired
	private BussFarmlandInfoService bussFarmlandInfoService;
	@Autowired
	private BussRtuEquipmentService bussRtuEquipmentService;
	@Autowired
	private BussRtuInfoService bussRtuInfoService;
	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private SysDeptService sysDeptService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("buss:bussmaintaintask:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = bussMaintainTaskService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("buss:bussmaintaintask:info")
	public R info(@PathVariable("id") String id) {
		BussMaintainTaskEntity bussMaintainTask = bussMaintainTaskService.getById(id);

		if (null != bussMaintainTask) {
			SysUserEntity sysUserEntity = sysUserService.getById(bussMaintainTask.getHandler());
			if (null != sysUserEntity) {
				bussMaintainTask.setHandlerName(sysUserEntity.getName());
			}

			// 部门信息
			SysDeptEntity deptEntity = sysDeptService.getById(bussMaintainTask.getDeptId());
			if(null != deptEntity){
				bussMaintainTask.setDeptName(deptEntity.getName());
			}

			// 设备信息
			BussRtuEquipmentEntity bussRtuEquipmentEntity = bussRtuEquipmentService
					.getById(bussMaintainTask.getEquipmentId());
			if (null != bussRtuEquipmentEntity) {
				bussMaintainTask.setEquipmentName(bussRtuEquipmentEntity.getName());
				BussRtuInfoEntity rtuInfoEntity = bussRtuInfoService.getById(
						bussRtuEquipmentEntity.getRtuId());
				if(null!= rtuInfoEntity){
					bussMaintainTask.setRtuSerialNum(rtuInfoEntity.getSerialNum());
					BussFarmlandInfoEntity farmlandInfoEntity = bussFarmlandInfoService.getById(
							rtuInfoEntity.getFarmlandInfoId());
					if(null != farmlandInfoEntity){
						bussMaintainTask.setRtuFarmlandName(farmlandInfoEntity.getName());
					}
				}
			}

			// 紧急程度（字典）
			if (StringUtils.isNotBlank(bussMaintainTask.getGradeNum())) {
				SysDictEntity dict = sysDictService.getById(bussMaintainTask.getGradeNum());
				if (dict != null && StringUtils.isNotBlank(dict.getValue())) {
					bussMaintainTask.setGradeNumName(dict.getValue());
				}
			}


		}

		return R.ok().put("bussMaintainTask", bussMaintainTask);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("buss:bussmaintaintask:save")
	public R save(@RequestBody BussMaintainTaskEntity bussMaintainTask) {
		ValidatorUtils.validateEntity(bussMaintainTask, AddGroup.class);

		bussMaintainTask.setCreateId(ShiroUtils.getUserId() + "");
		bussMaintainTask.setCreateName(ShiroUtils.getUserName());
		bussMaintainTask.setCreateTime(new Date());

		// 从设备获取所属基地与田块
		BussRtuEquipmentEntity bussRtuEquipmentEntity = bussRtuEquipmentService
				.getById(bussMaintainTask.getEquipmentId());
		if (null != bussRtuEquipmentEntity) {
			bussMaintainTask.setDeptId(bussRtuEquipmentEntity.getDeptId());

			BussRtuInfoEntity bussRtuInfoEntity = bussRtuInfoService.getById(bussRtuEquipmentEntity.getRtuId());
			if (null != bussRtuInfoEntity) {
				bussMaintainTask.setFarmlandInfoId(bussRtuInfoEntity.getFarmlandInfoId());
			}

		}

		// 生成工号
		String ymdStr = DateUtils.format(new Date(), "yyyyMMdd");// 年月日
		String businessNum = ymdStr + "G0001";
		BussMaintainTaskEntity lastBussMaintainTaskEntity = bussMaintainTaskService
				.getOne(new QueryWrapper<BussMaintainTaskEntity>().select("max(business_num) as business_num")
						.like("business_num", ymdStr));
		if (null != lastBussMaintainTaskEntity) {
			String lastNumStr = lastBussMaintainTaskEntity.getBusinessNum().substring(9);
			Integer lastNum = Integer.valueOf(lastNumStr) + 1;
			businessNum = ymdStr + "G" + String.format("%04d", lastNum);
		}
		bussMaintainTask.setBusinessNum(businessNum);

		bussMaintainTaskService.save(bussMaintainTask);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("buss:bussmaintaintask:update")
	public R update(@RequestBody BussMaintainTaskEntity bussMaintainTask) {
		ValidatorUtils.validateEntity(bussMaintainTask, AddGroup.class);

		bussMaintainTask.setUpdateId(ShiroUtils.getUserId() + "");
		bussMaintainTask.setUpdateName(ShiroUtils.getUserName());
		bussMaintainTask.setUpdateTime(new Date());

		// 从设备获取所属基地与田块
		BussRtuEquipmentEntity bussRtuEquipmentEntity = bussRtuEquipmentService
				.getById(bussMaintainTask.getEquipmentId());
		if (null != bussRtuEquipmentEntity) {
			bussMaintainTask.setDeptId(bussRtuEquipmentEntity.getDeptId());

			BussRtuInfoEntity bussRtuInfoEntity = bussRtuInfoService.getById(bussRtuEquipmentEntity.getRtuId());
			if (null != bussRtuInfoEntity) {
				bussMaintainTask.setFarmlandInfoId(bussRtuInfoEntity.getFarmlandInfoId());
			}
		}

		// 填入接收时间与完成时间
		if (bussMaintainTask.getStatus() == ProcessStateEnum.PROCESSING.getValue()) {
			bussMaintainTask.setReceiveTime(new Date());
		} else if (bussMaintainTask.getStatus() == ProcessStateEnum.FINISH.getValue()) {
			bussMaintainTask.setFinishTime(new Date());
		}

		Boolean re = bussMaintainTaskService.updateById(bussMaintainTask);// 全部更新

		return re ? R.ok() : R.oldVersion();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("buss:bussmaintaintask:delete")
	public R delete(@RequestBody String[] ids) {
		bussMaintainTaskService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

}
